home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / 8bit / cislib_b / strarr.doc < prev    next >
Text File  |  1995-04-22  |  4KB  |  105 lines

  1. .12 
  2. 4 1 5 0 10 70 2 12 132 
  3. 3STRING ARRAYS FOR THE ATARI 
  4. .1by  
  5. .David H. Neal 
  6. .70707,724 
  7. Arvada, CO  
  8. .4A number of programs have been written to simulate string arrays on the 
  9. Atari computers. Here is another one, perhaps a bit more elegant than some, 
  10. in that it avoids filling unused portions of the array with blanks. This 
  11. one has no unused areas. Basically, the technique involves finding the 
  12. delimiters for string segmenting; i.e., in A$(x,y), we will find x and 
  13. y for each variable in the array and use x and y to select the variable 
  14. desired. I will offer two examples to demonstrate the technique. 
  15. .The first example is representative of the case where we have a known, 
  16. fixed number of elements of equal length, as for example, in a list of 
  17. the months of the year, abbrviated to three letters. The complete string 
  18. is 
  19. MONTH$="JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC" 
  20. The individual element might be called MON$,and we might want to list the 
  21. name of the numerical month, M. We might call the delimiters M1 and M2, 
  22. so that MON$ is given by 
  23. MON$ = MONTH$(M1,M2) 
  24. For elements of 3 letters, M1 is 
  25. M1 = M + 2 * (m-1) 
  26. and M2 = M1 + 2 
  27. A program to list the numerical month with its 3-letter abbrviation would 
  28. be: 
  29. 10 DIM MONTH$(36),MON$(3) 
  30. 20 MONTH$="JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC" 
  31. 30 ?"ENTER THE NUMERICAL MONTH";:INPUT M 
  32. 40 M1=M+2*(M-1):M2=M1+2 
  33. 50 MON$=MONTH$(M1,M2) 
  34. 60 ? M,MON$ 
  35. 70 GOTO 30 
  36. .M1 and M2 can be similarly derived for any situation where a full string 
  37. can be composed of elements of equal length. A general equation for M1 
  38. and M2 is: 
  39. M1 = M + A*(M-1) 
  40. M2 = M1 + A 
  41. where A is 1 less than the length of the elements. 
  42. .The second case is one in which we have an unknown number of elements and 
  43. they are of variable length. This is the case that has traditionally been 
  44. handled by filling a string with spaces. An example might be the entering 
  45. of names and their random recall. We can apply the same principle that 
  46. we used for the months example, except that now we are forced to deal with 
  47. an unknown element length. 
  48. .Let us suppose that our program requirements are 10 names, and we have 
  49. decided to allow each name to be up to 10 characters (including spaces) 
  50. long. So, 
  51. DIM ALLNAM$(100),NAME$(10) 
  52. We also need, for this case, two arrays, L1 and L2 of 10 elements each. 
  53. So, 
  54. DIM L1(10),L2(10) 
  55. At the beginning, the total length of our string is zero, so 
  56. TOL=0 
  57. For each name that we input in succession, the beginning element is the 
  58. total length of our string so far plus 1, or 
  59. L1(I) = TOL + 1 
  60. and the ending element is L1 plus the length of the current name minus 
  61. 1, or 
  62. L2(I) = L1(I) + LEN(NAME$) - 1 
  63. Now the total length of our string is 
  64. TOL = TOL + LEN(NAME$) 
  65. and the portion of ALLNAM$ that holds the name we just entered is 
  66. ALLNAM$(L1(I),L2(I)) = NAME$ 
  67. We can now write a complete program segment for an array of 10 names, each 
  68. a maximum length of 10 characters. 
  69. 10 DIM ALLNAM$(100),NAME$(10),L1(10),L2(10) 
  70. 20 TOL=0 
  71. 30 ?"ENTER NUMBER OF NAMES UP TO 10";:INPUT N 
  72. 40 IF N>10 THEN 30 
  73. 50 FOR I=1 TO N 
  74. 60 ?"ENTER NAME";:INPUT NAME$ 
  75. 70 L1(I)=TOL+1:L2(I)=L1(I)+LEN(NAME$)-1:TOL=TOL+LEN(NAME$) 
  76. 80 ALLNAM$(L1(I),L2(I))=NAME$:NEXT I 
  77. Now we need a method to get back the names we entered. if we know the sequenti 
  78. al number all we need is 
  79. NAME$=ALLNAM$(L1(I),L2(I)) 
  80. But, we may want to match a particular name with some data in another array. 
  81. We have to do a search as in the following program segment: 
  82. 90 ?"ENTER NAME";:INPUT NAME$ 
  83. 100 FOR I=1 TO N:IF NAME$=ALLNAM$(L1(I),L2(I)) THEN 120 
  84. 110 NEXT I:?"NAME NOT FOUND--TRY AGAIN":GOTO 90 
  85. 120 ?"NAME FOUND" 
  86. .These methods comprise a direct, easily used technique for simulating string 
  87. arrays on the Atari home computers. 
  88. -30- 
  89.  
  90.